package com.itheima.orm.utils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ReflectUtil {
	//获取类名
	public static String getClassName(String className){
		if(null==className||className.trim().equals("")){
			return null;
		}else{
			return className.substring(className.lastIndexOf(".")+1);
		}
	}
	
	
	private static final Set<String> primitiveTypes 
	= new HashSet<>(Arrays.asList("boolean","byte","char","short","int","long","float","double","void"
			,"Boolean","Character","Byte","Short","Integer","Long","Float","Double","Void"));
	
	//获取字段
	public static Map<String, String> getFieldsAndTypes(String className){
		try {
			Field[] declaredFields = Class.forName(className).getDeclaredFields();
			Map<String, String> fieldsAndTypes = new HashMap<>();
			for (Field field : declaredFields) {
				String fieldName = field.getName();
				//暂只处理基本类型
				boolean isPrimitive = field.getType().isPrimitive();
				if(!isPrimitive){
//					isPrimitive = null!=field.getType().getDeclaredField("TYPE")?true:false;
					String fieldType = field.getType().getSimpleName();
					isPrimitive = primitiveTypes.contains(fieldType);
				}
				if(isPrimitive || "String".equals(field.getType().getSimpleName()) || "Date".equals(field.getType().getSimpleName())){
					String fieldType = field.getType().getSimpleName();
					fieldsAndTypes.put(fieldName, fieldType);
				}
			}
			return fieldsAndTypes;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
